package com.fs.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.controller.BaseController;
import com.fs.mapper.GoodsCartMapper;
import com.fs.mapper.GoodsMapper;
import com.fs.pojo.GoodsCart;
import com.fs.pojo.Goods;
import com.fs.service.GoodsCartService;
import com.fs.vo.CommRequestVO;
import com.fs.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

@Service
public class GoodsCartSerivceImpl extends BaseController implements GoodsCartService {
    @Autowired
    private GoodsCartMapper goodsCartMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    @Transactional
    public ResultVO add(GoodsCart entity) {
        Integer userId = getUserId();
        if (entity.getNum() < 1) {
            return ResultVO.fail("数量有误");
        }

        //查找商品
        Goods goodsEntity = goodsMapper.selectById(entity.getGoodsId());
        if (goodsEntity == null) {
            return ResultVO.fail("商品有误");
        }

        Date now = new Date();

        //查找购物车里面是否有重复的
        QueryWrapper<GoodsCart> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId)
                .eq("goods_id", entity.getGoodsId())
                .eq("status", false)
                .orderByDesc("id");
        GoodsCart goodsCart = null;
        try {
            goodsCart = goodsCartMapper.selectOne(queryWrapper);
            if (goodsCart != null) {
                goodsCart.setNum(entity.getNum() + goodsCart.getNum())
                        .setUpdated(now);
                goodsCartMapper.updateById(goodsCart);
            } else {
                entity.setPrice(goodsEntity.getPrice())
                        .setUserId(userId)
                        .setGoodsId(goodsEntity.getId())
                        .setCreated(now)
                        .setUpdated(now);
                goodsCartMapper.insert(entity);
            }
            return ResultVO.success();
        } catch (Exception e) {
//            e.printStackTrace();
            return ResultVO.fail("购物车重复");
        }


    }

    @Override
    @Transactional
    public ResultVO edit(GoodsCart entity) {
        goodsCartMapper.updateById(entity);
        return ResultVO.success();
    }

    @Override
    public ResultVO list(CommRequestVO request) {
//        if (request == null) return ResultVO.fail();
//
//        int pageNum = request.getPageNum();
//        int pageSize = request.getPageSize();
//
//        if (pageNum < 1) pageNum = 1;
//        if (pageSize < 1) pageNum = 10;
//
//        Page<GoodsCartEntity> page = new Page<>(pageNum, pageSize);
//
//
//        QueryWrapper<GoodsCartEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like(StrUtil.isNotBlank(request.getQuery()), "username", request.getQuery())
//                .orderByDesc("id");
//        page = goodsCartDao.selectPage(page, queryWrapper);
//        request.setTotal(page.getTotal()).setRows(page.getRecords());

//        QueryWrapper<GoodsCartEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("status", false)
//                .eq("user_id", getUserId())
//                .orderByDesc("updated");
//        goodsCartDao.selectList(queryWrapper);

        List<Map<String, Object>> list = goodsCartMapper.selectCartList(getUserId());
        return ResultVO.success(list);
    }

    @Override
    @Transactional
    public ResultVO delById(Integer id) {
        goodsCartMapper.deleteById(id);
        return ResultVO.success();
    }

    @Transactional
    @Override
    public ResultVO del(Integer[] ids) {

        QueryWrapper<GoodsCart> queryWrapper = new QueryWrapper();
        queryWrapper.in("id", Arrays.asList(ids))
                .eq("user_id", getUserId())
                .eq("status", false);
        goodsCartMapper.delete(queryWrapper);
        return ResultVO.success();
    }

    @Override
    public ResultVO delAll() {

        QueryWrapper<GoodsCart> queryWrapper = new QueryWrapper();
        queryWrapper
                .eq("user_id", getUserId())
                .eq("status", false);
        goodsCartMapper.delete(queryWrapper);
        return ResultVO.success();
    }

    @Transactional
    @Override
    public ResultVO changeNum(GoodsCart goodsCartEntity) {
        QueryWrapper<GoodsCart> wrapper = new QueryWrapper();
        wrapper.eq("status", false)
                .eq("user_id", getUserId())
                .eq("goods_id", goodsCartEntity.getGoodsId())
                .eq("id", goodsCartEntity.getId());
        goodsCartMapper.update(goodsCartEntity, wrapper);
        return ResultVO.success();
    }


}
